# !/usr/bin/env python
# -*- coding: UTF-8 -*-
# date: 2/7/12 6:16 PM
# Filename: client_open_close.py

#
# Copyright 2011 Youku
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.



import ConfigParser
import datetime
import time
from constants import RESP_CODE
from handlers.handler import BaseHandler
from util import log

### read configurations from conf/stat.cfg ###
_config = ConfigParser.RawConfigParser()
_config.read('../conf/stat.cfg')

_section = 'DataFile'
_path = _config.get(_section, 'path')
_filename = _config.get(_section, 'filename')
_timeformat = _config.get(_section, 'timeformat')

### global variables ###
_log = log.getLogger()
_enable = _config.get(_section, 'enable')
_now = datetime.datetime.now()
_data_file_path = _path + _filename.format(time.strftime(_timeformat))

class ClientOCStatHandler(BaseHandler):
    '''
    负责接受客户端开启、关闭统计数据的接受

    action	 操作行为（start、quit、toback 、tofront）	 Not NULL
    time	 操作时间（yyyy-MM-dd HH:mm:ss）	 Not NULL
    position	 位置信息（经纬度），start和tofront是传递该参数

    '''

    def get(self, *args, **kwargs):
        _log.info('class:ClientOCStatHandler method:get()')

        if self._finished:
            return None

        # step 1 : get all arguments:
        action = self.get_argument('action', None)
        time = self.get_argument('time', None)
        position = self.get_argument('position', None)

        # step 2 : verify all arguments:
        if not action:
            self.writeAndFinish(RESP_CODE.resp_code_invalid_action)
        elif not time:
            self.writeAndFinish(RESP_CODE.resp_code_invalid_time)
        elif not position:
            self.writeAndFinish(RESP_CODE.resp_code_invalid_position)

        if self._finished:
            return None

        # step 3 : record the argument to local util file , according to the LogSpecification
        if _enable:
            # TODO implement
            pass
        self.writeAndFinish(RESP_CODE.resp_code_success)

handlers = [(ur'/stat/3', ClientOCStatHandler)]